shortcuts.spec.js ➔ ???   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 1
c 4
b 0
f 0
nc 1
dl 0
loc 56
rs 9.7251
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import test from 'ava';
2
import {
3
    GET,
4
    HEAD,
5
    POST,
6
    PUT,
7
    DELETE,
8
    CONNECT,
9
    OPTIONS,
10
    TRACE,
11
    PATCH
12
} from '../src/shortcuts';
13
14
const dummy = {
15
    path: ['path', 'to', 'resource'],
16
    query: { param: 'value' },
17
    options: { credentials: 'include' },
18
    type: 'text'
19
};
20
21
test('should set correct HTTP method', t => {
22
    t.deepEqual(GET(dummy), {
23
        path: ['path', 'to', 'resource'],
24
        query: { param: 'value' },
25
        options: { credentials: 'include', method: 'GET' },
26
        type: 'text'
27
    }, 'GET');
28
    t.deepEqual(HEAD(dummy), {
29
        path: ['path', 'to', 'resource'],
30
        query: { param: 'value' },
31
        options: { credentials: 'include', method: 'HEAD' },
32
        type: 'text'
33
    }, 'HEAD');
34
    t.deepEqual(POST(dummy), {
35
        path: ['path', 'to', 'resource'],
36
        query: { param: 'value' },
37
        options: { credentials: 'include', method: 'POST' },
38
        type: 'text'
39
    }, 'POST');
40
    t.deepEqual(PUT(dummy), {
41
        path: ['path', 'to', 'resource'],
42
        query: { param: 'value' },
43
        options: { credentials: 'include', method: 'PUT' },
44
        type: 'text'
45
    }, 'PUT');
46
    t.deepEqual(DELETE(dummy), {
47
        path: ['path', 'to', 'resource'],
48
        query: { param: 'value' },
49
        options: { credentials: 'include', method: 'DELETE' },
50
        type: 'text'
51
    }, 'DELETE');
52
    t.deepEqual(CONNECT(dummy), {
53
        path: ['path', 'to', 'resource'],
54
        query: { param: 'value' },
55
        options: { credentials: 'include', method: 'CONNECT' },
56
        type: 'text'
57
    }, 'CONNECT');
58
    t.deepEqual(OPTIONS(dummy), {
59
        path: ['path', 'to', 'resource'],
60
        query: { param: 'value' },
61
        options: { credentials: 'include', method: 'OPTIONS' },
62
        type: 'text'
63
    }, 'OPTIONS');
64
    t.deepEqual(TRACE(dummy), {
65
        path: ['path', 'to', 'resource'],
66
        query: { param: 'value' },
67
        options: { credentials: 'include', method: 'TRACE' },
68
        type: 'text'
69
    }, 'TRACE');
70
    t.deepEqual(PATCH(dummy), {
71
        path: ['path', 'to', 'resource'],
72
        query: { param: 'value' },
73
        options: { credentials: 'include', method: 'PATCH' },
74
        type: 'text'
75
    }, 'PATCH');
76
});
77
78
test('should add method even when nothing is passed', t => {
79
    const result = GET();
80
    t.deepEqual(result, { options: { method: 'GET'} });
81
});
82
83
test('should not take precedence over passed params', t => {
84
    const result = PUT({ options: { method: 'POST' } });
85
    t.deepEqual(result, { options: { method: 'POST'} });
86
});